package com.hsd.znsh.service.impl;

import com.hsd.znsh.converter.FarmConverter;
import com.hsd.znsh.entity.Farm;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.pojo.output.FarmVO;
import com.hsd.znsh.pojo.query.FarmQuery;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.repository.FarmRepository;
import com.hsd.znsh.repository.FarmTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.service.FarmService;
import com.hsd.znsh.utils.DateUtil;
import com.hsd.znsh.utils.FileUtil;
import com.hsd.znsh.utils.HqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 15:54:52
 */
@Service
public class FarmServiceImpl extends BaseServiceImpl implements FarmService{

    @Autowired
    private FarmRepository farmRepository;

    @Autowired
    private FarmTypeRepository farmTypeRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public long getFarmListTotalPages(FarmQuery farmQuery, Integer userId) {
        if(userId!=null) farmQuery.setCreateUserId(userId);
        return this.count(HqlUtil.getCountHql(farmQuery),farmQuery);
    }

    @Override
    public List<FarmVO> getFarmList(Pageable pageable, FarmQuery farmQuery, Integer userId) {
        if(userId!=null) farmQuery.setCreateUserId(userId);
        List<Farm> list=this.find(HqlUtil.getQueryHql(farmQuery),farmQuery,pageable);
        return FarmConverter.converterToFarmVOList(list,userRepository,farmTypeRepository);
    }

    @Override
    public List<FarmVO> getFarmList(FarmQuery farmQuery, Integer userId) {
        if(userId!=null) farmQuery.setCreateUserId(userId);
        List<Farm> list=this.find(HqlUtil.getQueryHql(farmQuery),farmQuery);
        return FarmConverter.converterToFarmVOList(list,userRepository,farmTypeRepository);
    }

    @Override
    public List<Map<String,Object>> getFarmRecord(Integer farmTypeId, Integer totalTime) {
        List<Farm> list=farmRepository.findByFarmTypeIdAndCreateDateTimeAfter(farmTypeId, DateUtil.getPrevDay(totalTime));
        List<Map<String,Object>> mapList=new ArrayList<>();
        for(Farm farm:list){
            Map<String,Object> map=new HashMap<>();
            map.put("record",DateUtil.format(farm.getCreateDateTime(),"yyyy-MM-dd HH:mm:ss")+" "+farm.getName());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<FarmVO> getFarmListByBatchId(Integer batchId, Integer farmTypeId) {
        List<Farm> list;
        if(farmTypeId==null){
            list=farmRepository.findByBatchIdOrderByCreateDateTimeDesc(batchId);
        }else{
            list=farmRepository.findByBatchIdAndFarmTypeIdOrderByCreateDateTimeDesc(batchId,farmTypeId);
        }
        return FarmConverter.converterToFarmVOList(list,userRepository,farmTypeRepository);
    }

    @Override
    public Farm addFarm(MultipartFile multipartFile, Farm farm,Integer userId) {
        farm.setCreateUserId(userId);
        farm=farmRepository.save(farm);
        if(multipartFile!=null){
            String filepath=FileUtil.copyToFile(multipartFile,securityProperties.getBrowser().getFilePath()+"/farm/"+farm.getId());
            farm.setPicture(filepath);
        }
        return farmRepository.save(farm);
    }

    @Override
    public void deleteFarm(Integer id) {
        Farm farm=farmRepository.findOne(id);
        if(farm.getPicture()!=null&&!farm.getPicture().equals("")){
            FileUtil.delFile(farm.getPicture());
        }
        farmRepository.delete(id);
    }

    @Override
    public FarmVO getFarmInfo(Integer id) {
        Farm farm=farmRepository.findOne(id);
        return FarmConverter.converterToFarmVO(farm,userRepository,farmTypeRepository);
    }
}
